package com.gitee.feizns.quickstart.mybatis.plus.injector.sql;

import com.baomidou.mybatisplus.annotation.FieldStrategy;
import com.baomidou.mybatisplus.core.injector.AbstractMethod;
import com.baomidou.mybatisplus.core.injector.DefaultSqlInjector;
import com.baomidou.mybatisplus.core.injector.methods.SelectMapsPage;
import com.baomidou.mybatisplus.core.injector.methods.SelectPage;
import com.baomidou.mybatisplus.core.mapper.Mapper;
import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.extension.injector.methods.AlwaysUpdateSomeColumnById;
import com.baomidou.mybatisplus.extension.injector.methods.InsertBatchSomeColumn;

import java.util.List;

/**
 * 添加的从实体映射到{@link Mapper}
 * @author feizns
 * @since 2023/2/12
 */
public class QuickstartMybatisPlusSqlInjector extends DefaultSqlInjector {

    /**
     * Get 方法列表
     * @param mapperClass Mapper 类
     * @param tableInfo 表信息
     * @return {@link List }<{@link AbstractMethod }>
     */
    @SuppressWarnings("deprecation")
    @Override
    public List<AbstractMethod> getMethodList(Class<?> mapperClass, TableInfo tableInfo) {
        List<AbstractMethod> abstractMethods = super.getMethodList(mapperClass, tableInfo);
        //添加批量新增方法
        abstractMethods.add(new InsertBatchSomeColumn());
        //添加分页查询
        abstractMethods.add(new SelectPage());
        //添加分页查询
        abstractMethods.add(new SelectMapsPage());
        //添加分页查询
        abstractMethods.add(new AlwaysUpdateSomeColumnById(QuickstartMybatisPlusSqlInjector::alwaysUpdateSomeColumnByIdIgnoreFields));
        return abstractMethods;
    }


    /**
     * 总是更新一些列由编号忽视领域
     * @param fieldInfo 字段信息
     * @return boolean
     */
    public static boolean alwaysUpdateSomeColumnByIdIgnoreFields(TableFieldInfo fieldInfo) {
        return !fieldInfo.isVersion()
                && !fieldInfo.isLogicDelete()
                && !(fieldInfo.isWithUpdateFill())
                && fieldInfo.getUpdateStrategy() != FieldStrategy.NEVER;
    }

}
